package com.lh.controller.dev;

import com.aliyuncs.exceptions.ClientException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lh.domain.Dictionary;
import com.lh.domain.Info;
import com.lh.service.DictionaryService;
import com.lh.service.InfoService;
import com.lh.utils.alTool.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author LH
 * @Date 2023/10/30 10:53
 * @PackageName com.lh.controller.dev
 * @Description: [项目名小写 多个单词用_分隔 | 包名全部小写 | 类 首字母大写 | 变量和方法 首字母小写，多个单词组成时除首字母，其他字母首字母需大写 | 常量 全部大写,多个单词用'_'分割]
 */
@Controller
@RequestMapping("/dev/flatform/app")
public class DevAdd {
    //http://localhost:8080/gameproject_war_exploded/dev/flatform/app/list
    //http://localhost:8080/gameproject_war_exploded/dev/flatform/app/appinfoadd

    @Autowired
    private DictionaryService dictionary;

    @Autowired
    private InfoService infoService;

    //添加页面初始化
    @RequestMapping("/datadictionarylist")
    @ResponseBody
    public String addInit(HttpServletRequest request) throws JsonProcessingException {
        String tCode = request.getParameter("tcode");
        List<Dictionary> dictionaries = dictionary.selectValueName(tCode);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(dictionaries);
    }

    @RequestMapping("/appinfoaddsave")
    @ResponseBody
    public void saveInfo(HttpServletRequest request, @RequestParam("a_logoPicPath") MultipartFile file) throws ServletException, IOException, ClientException {
        System.out.println("保存启动了");

        Info info = new Info();
        //没有技术,用笨的方法
        info.setSoftwareName(request.getParameter("softwareName"));//1
        info.setAPKName(request.getParameter("APKName"));
        info.setSupportROM(request.getParameter("supportROM"));
        info.setInterfaceLanguage(request.getParameter("interfaceLanguage"));
        info.setSoftwareSize(Long.valueOf(request.getParameter("softwareSize")));//5
        info.setDownloads(Long.valueOf(request.getParameter("downloads")));
        info.setFlatformId(Long.valueOf(request.getParameter("flatformId")));
        info.setCategoryLevel1(request.getParameter("categoryLevel1"));
        info.setCategoryLevel2(request.getParameter("categoryLevel2"));
        info.setCategoryLevel3(request.getParameter("categoryLevel3"));
        info.setStatus(Long.valueOf(request.getParameter("status")));
        info.setAppInfo(request.getParameter("appInfo"));//12

        //特性的属性
        //开发者id
        info.setDevId(1L);//登录做完后修改session或redis
        //创建者id
        info.setCreatedBy(1L);
        //创建时间
        info.setCreationDate(new Date());
        //图片路径
        String type = file.getContentType();
        //        image/png
        String fileName = UUID.randomUUID() +"."+type.substring(type.lastIndexOf("/")+1);
        String path = "/statics/uploadfiles/"+ fileName;
        info.setLogoPicPath(path);

        //服务器路径--重命名文件避免覆盖
        String realPath = request.getSession().getServletContext().getRealPath("").replace("\\","/");
        realPath = realPath.substring(0,realPath.lastIndexOf("/"));
//        真实路径:E:\ssm_project\gameproject\out\artifacts\gameproject_war_exploded\
        System.out.println(realPath+ path);

        info.setLogoLocPath(realPath);

        int i =  infoService.addInfo(info);

        if(i > 0){
            //开始临时文件
            file.transferTo(new File(realPath+path));
            //存内存文件->不写了,因为内存不固定,直接存oss里面,查询那边如果获取图片没有就会用oss下载
            FileUpload fileUpload = new FileUpload();
            fileUpload.upload(path.replaceFirst("/",""),file.getInputStream());
        }else{
            System.out.println(i+"结果不对");
            throw new RuntimeException();
        }
        //应该存2个地方,避免删除影响--->本地删除了才会加载云端OSS
        //E:\ssm_project\gameproject\out\artifacts\gameproject_war_exploded\
        //src\\main\\web\\uploadfiles

        System.out.println(info);
//        return "developer/appinfolist";
    }
}
